﻿module Sustainalytics.Portfolio {

    import Common = Sustainalytics.Common;

    export class PortfolioSearchService {

        private $http: ng.IHttpService;
        private $rootScope: ng.IScope;
        private accountId: string;
        private userId: string;
        private $q: ng.IQService;
        private porfMixDto: PortfolioUniverseMixDto;


        //private _staticObjects: Common.IStaticObjects;

         constructor($http: ng.IHttpService, $rootScope: ng.IScope, $q: ng.IQService/*, staticObjects: Common.IStaticObjects*/) {
            this.$http = $http;
            this.$rootScope = $rootScope;
            //this._staticObjects = staticObjects;


            if (this.$rootScope['userInfo'] == null) {
                this.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }

            if (this.$rootScope['userInfo'] == null) {
                //this._staticObjects.redirectToLogin();
                return;
            }
            this.$q = $q;

            this.porfMixDto = null;
        }

         public SearchPortfolio(term: string, skip: number, take: number, ptype: PortfolioTypes = PortfolioTypes.Custom, status: PortfolioSharingStatus = PortfolioSharingStatus.All): ng.IHttpPromise<PortfolioDto[]> {

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = ["Name"];
            options.orderAscending = true;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({
                isMyPortfolio: true,
                namePortfolio: term,
                sharingStatus: status,
                portfolioType: ptype,
                isNameEqual: false,
                accountId: this.$rootScope['userInfo'].accountId,
                userId: this.$rootScope['userInfo'].userId,
                listOptions: options
            });

            var url: string = '/portfolioserv/api/v1/portfolios?' + params;

            var lstPromise: ng.IHttpPromise<PortfolioDto[]> = this.$http.get(url);

            return lstPromise;
        }

       public SearchScreeningToolPortfolio(term: string, skip: number, take: number): ng.IHttpPromise<PortfolioDto[]> {       

            var options = new Common.ListOptions();
            options.skip = skip;
            options.take = take;
            options.orderBy = ["Name"];
            options.orderAscending = true;
            options.includeAllFields = false;
            options.getCount = false;

            var params = $.param({
                isMyPortfolio: null,
                namePortfolio: term,
                sharingStatus: null,
                isNameEqual: false,
                accountId: this.$rootScope['userInfo'].accountId,
                userId: this.$rootScope['userInfo'].userId,
                listOptions: options,
                screeningTool: true
            });

            var url: string = '/portfolioserv/api/v1/portfolios?' + params;

            //Added
            var portfolios: PortfolioDto[] = null;

            //Original
            //var lstPromise: ng.IHttpPromise<PortfolioDto[]> = this.$http.get(url);

            //Modified with IPromise from  IHttpPromise
            var lstPromise: ng.IHttpPromise<PortfolioDto[]> = this.$http.get(url);

            lstPromise.then((data: PortfolioDto[]) => {
                portfolios = data;
            });

            return lstPromise;
        }

       public SearchScreeningToolPortfoliosAndUniversesMix(term: string, skip: number, take: number): ng.IHttpPromise<any[]>{
       
           var options = new Common.ListOptions();
           options.skip = skip;
           options.take = take;
           options.orderBy = ["Name"];
           options.orderAscending = true;
           options.includeAllFields = false;
           options.getCount = false;

           var params = $.param({
               isMyPortfolio: true,
               namePortfolio: term,
               sharingStatus: null,
               isNameEqual: false,
               accountId: this.$rootScope['userInfo'].accountId,
               userId: this.$rootScope['userInfo'].userId,
               listOptions: options

           });

           var portfolios: PortfolioDto[] = null;
           var universes: any = null;
           var portfoliosAndUniverses: any[] = [];
                   
           var url: string = "/screeningtoolserv/api/v1/portfolios/universesandportfolios/?" + params;
                    
          var portfoliosUniversesPromise: ng.IHttpPromise<any[]> = this.$http.get(url);
        
           portfoliosUniversesPromise.then((data: any[]) => {
               portfoliosAndUniverses = data;
           });

           return portfoliosUniversesPromise;          
          
       }

    }

    var portfolioSearchModule = angular.module(portfolioModuleName);

    portfolioSearchModule.service('PortfolioSearchService',
        ['$http', '$rootScope','$q', /*'staticObjects',*/ PortfolioSearchService]);
}